import 'package:ct_pa_majordomo/config/theme_vars.dart';
import 'package:ct_pa_majordomo/pages/home/noticeApi.dart';
import 'package:ct_pa_majordomo/utils/date_tools.dart';
import 'package:ct_pa_majordomo/widgets/normal_scaffold/index.dart';
import 'package:flutter/material.dart';

class NoticeDetail extends StatefulWidget {
  final Map<String, dynamic> arguments;
  
  const NoticeDetail({Key? key, required this.arguments}) : super(key: key);

  @override
  _NoticeDetailState createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  bool _isLoading = true;
  Map<String, dynamic> _noticeDetail = {};
  
  @override
  void initState() {
    super.initState();
    _fetchNoticeDetail();
  }
  
  // 获取消息详情
  Future<void> _fetchNoticeDetail() async {
    setState(() {
      _isLoading = true;
    });
    
    try {
      final notificationId = widget.arguments['notificationId'];
      print("notificationId:>>>$notificationId");
      final result = await noticeApi.getNoticeDetail(notificationId);
      
      if (result['code'] == 200) {
        setState(() {
          _noticeDetail = Map<String, dynamic>.from(result['data'] ?? {});
        });
        
        // 标记消息为已读
        _markAsRead(notificationId);
      }
    } catch (e) {
      print('Error fetching notice detail: $e');
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
  
  // 标记消息为已读
  Future<void> _markAsRead(int notificationId) async {
    try {
      await noticeApi.readNotice(notificationId);
    } catch (e) {
      print('Error marking notice as read: $e');
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: widget.arguments['title'] ?? '消息详情',
      child: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : SingleChildScrollView(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    _noticeDetail['title'] ?? '',
                    style: const TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 12),
                  Row(
                    children: [
                      Text(
                        _noticeDetail['sender'] ?? '系统消息',
                        style: TextStyle(
                          fontSize: 14,
                          color: ThemeVars.colorTextSec,
                        ),
                      ),
                      const SizedBox(width: 16),
                      Text(
                        DateTools.formatYMD(_noticeDetail['createTime']),
                        style: TextStyle(
                          fontSize: 14,
                          color: ThemeVars.colorTextSec,
                        ),
                      ),
                    ],
                  ),
                  const Divider(height: 32),
                  Text(
                    _noticeDetail['content'] ?? '',
                    style: const TextStyle(
                      fontSize: 16,
                      height: 1.6,
                    ),
                  ),
                  if (_noticeDetail['attachments'] != null && 
                      _noticeDetail['attachments'] is List && 
                      (_noticeDetail['attachments'] as List).isNotEmpty) ...[
                    const SizedBox(height: 24),
                    const Text(
                      '附件:',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 8),
                    ..._buildAttachments(),
                  ],
                ],
              ),
            ),
    );
  }
  
  // 构建附件列表
  List<Widget> _buildAttachments() {
    final List attachments = _noticeDetail['attachments'] as List;
    return attachments.map((attachment) {
      return Container(
        margin: const EdgeInsets.only(bottom: 8),
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.circular(4),
        ),
        child: Row(
          children: [
            const Icon(Icons.attachment, size: 20),
            const SizedBox(width: 8),
            Expanded(
              child: Text(
                attachment['name'] ?? '附件',
                style: const TextStyle(fontSize: 14),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      );
    }).toList();
  }
} 